var e = require, t = module, n = exports;

Object.defineProperty(n, "__esModule", {
    value: !0
});

var o = e("GameConfig"), i = e("GameData"), a = e("UIManager"), r = function() {
    function n() {
        this.customAdMap = new Map(), this.customAdCountMap = new Map(), this.customAdNodeMap = new Map(), 
        this.customAdExp = [ "msg", "toast" ], this.showList = [], this.hideList = [], this.handleTime = 200, 
        this.refreshTime = null, this.tid = null;
    }
    return n.getInstance = function() {
        return this._manager || (this._manager = new n()), this._manager;
    }, n.prototype.initCustomAd = function(t) {
        if (this.customAdMap.has(t.adUnitId)) {
            if (this.checkCustomAdVaild(t.tag)) {
                var e = this.customAdMap.get(t.adUnitId);
                return e._isDestory = !0, e;
            }
            return this.customAdMap.get(t.adUnitId);
        }
        return this.createCustomAd(t);
    }, n.prototype.createCustomAd = function(t) {
        if (GA.isPlatform(GA.Platform.WeChat)) {
            console.log("customAd init");
            try {
                var e = GA.PA.getSystemInfoSync(), o = GA.PA.createCustomAd({
                    adUnitId: t.adUnitId,
                    style: {
                        left: t.pos.x / e.designPixelRatio,
                        top: e.windowHeight - t.pos.y / e.designPixelRatio
                    }
                });
                return o ? (o.onLoad(function() {
                    console.log("customAd onLoad success");
                    try {
                        o._isLoad = !0;
                    } catch (t) {}
                    cc.systemEvent.emit("refreshCustomAd");
                }), o.onError(function(t) {
                    console.log("customAd onError with:" + t.errCode + "," + t.errMsg);
                }), this.customAdMap.set(t.adUnitId, o), o) : null;
            } catch (t) {
                return console.log("customAd init fail " + t), null;
            }
        } else {
            if (!GA.isPlatform(GA.Platform.QQ)) return null;
            try {
                var e = GA.PA.getSystemInfoSync(), i = GA.PA.createBlockAd({
                    adUnitId: t.adUnitId,
                    size: t.size || 1,
                    orientation: t.orientation || "landscape",
                    style: {
                        left: Math.max(16, Math.round(t.pos.x / e.designPixelRatio)),
                        top: Math.max(16, Math.round(e.windowHeight - t.pos.y / e.designPixelRatio))
                    }
                });
                return i ? (i.onLoad(function() {
                    console.log("customAd onLoad success");
                    try {
                        i._isLoad = !0;
                    } catch (t) {}
                    cc.systemEvent.emit("refreshCustomAd");
                }), i.onError(function(t) {
                    console.log("customAd onError with:" + t.errCode + "," + t.errMsg);
                }), this.customAdMap.set(t.adUnitId, i), i) : null;
            } catch (t) {
                return console.log("customAd init fail " + t), null;
            }
        }
    }, n.prototype.destoryCustomAd = function(t) {
        try {
            this.customAdMap.has(t) && (this.customAdMap.get(t).destroy(), this.customAdMap.delete(t));
        } catch (t) {}
    }, n.prototype.checkCustomAdVaild = function(t) {
        if (GA.isPlatform([ GA.Platform.WeChat, GA.Platform.QQ ])) {
            var e = this.getConfig(t);
            if (e && 0 < e.interval) {
                if (this.customAdCountMap.has(t)) {
                    var o = this.customAdCountMap.get(t);
                    return o < e.interval - 1 ? (this.customAdCountMap.set(t, o + 1), !1) : (this.customAdCountMap.set(t, 0), 
                    !0);
                }
                return this.customAdCountMap.set(t, 1), !1;
            }
        }
        return !1;
    }, n.prototype.checkCustomAd = function(e) {
        if (GA.isPlatform([ GA.Platform.WeChat, GA.Platform.QQ ])) {
            var t = o.GC.customAdConfigList.filter(function(t) {
                return t.tag == e;
            });
            if (0 < t.length && 0 != t[0].flag && n.getInstance().checkExtraCount(e)) return !0;
        }
        return !1;
    }, n.prototype.init = function() {
        GA.isPlatform([ GA.Platform.WeChat, GA.Platform.QQ ]) && (cc.systemEvent.on("refreshCustomAd", this.doRefresh, this), 
        cc.systemEvent.on("showCustomAd", this.doShow, this), cc.systemEvent.on("hideCustomAd", this.doHide, this));
    }, n.prototype.doUpdate = function(t) {
        null != this.refreshTime && (this.refreshTime > t ? this.refreshTime -= t : (null != this.tid && clearInterval(this.tid), 
        this.refreshTime = null, this.refresh()));
    }, n.prototype.doRefresh = function() {
        var t = this;
        null == this.refreshTime ? (this.refreshTime = this.handleTime, null != this.tid && clearInterval(this.tid), 
        this.tid = setInterval(function() {
            t.doUpdate(100);
        }, 100)) : this.refreshTime = this.handleTime;
    }, n.prototype.doShow = function(t) {
        t = this.hideList.indexOf(t);
        -1 < t && this.hideList.splice(t, 1);
    }, n.prototype.doHide = function(t) {
        this.hideList.includes(t) || this.hideList.push(t);
    }, n.prototype.refresh = function() {
        var o = this;
        this.showList = [];
        var e = a.default.getInstance().viewQueue.filter(function(t) {
            return !o.customAdExp.includes(t.text);
        });
        0 < e.length && this.customAdNodeMap.forEach(function(t) {
            t.bindView.uuid != e[e.length - 1].uuid || (t = o.getOption(t.tag)) && !o.showList.includes(t.adUnitId) && o.showList.push(t.adUnitId);
        }), console.warn("viewQuue", e, "showList", this.showList, "customAdList", Array.from(this.customAdNodeMap.values())), 
        this.customAdMap.forEach(function(t, e) {
            o.showList.includes(e) ? t && t._isLoad && (t.isShow() ? console.log("customAd already show " + e) : (console.log("customAd show " + e), 
            t.show())) : t && t._isLoad && (t.isShow() ? (console.log("customAd hide " + e), 
            t.hide().then(function() {
                t._isDestory && (t._isDestory = !1, setTimeout(function() {
                    console.log("customAd destory " + e), o.destoryCustomAd(e);
                    var t = o.getOptionByAdUnitId(e);
                    t && o.createCustomAd({
                        adUnitId: e,
                        pos: cc.v2(cc.winSize.width / 2 + t.x, cc.winSize.height / 2 + t.y),
                        size: t.size,
                        orientation: t.orientation
                    });
                }, 200));
            })) : console.log("customAd already hide " + e));
        });
    }, n.prototype.add = function(t, e) {
        n.getInstance().customAdNodeMap.set(t, e);
    }, n.prototype.remove = function(t) {
        n.getInstance().customAdNodeMap.delete(t);
    }, n.prototype.checkExtraCount = function(e) {
        var t = o.GC.customAdExtraConfigList.filter(function(t) {
            return t.tag == e;
        });
        if (0 < t.length) {
            var n = t[0], t = i.GD.cutomAdDataList.filter(function(t) {
                return t.tag == e;
            });
            if (0 < t.length) {
                t = t[0];
                return !(0 < n.rule.length) || "banner" !== n.rule[t.idx % n.rule.length].type;
            }
        }
        return !0;
    }, n.prototype.updateExtraCount = function(n) {
        var s, t, e = o.GC.customAdExtraConfigList.filter(function(t) {
            return t.tag == n;
        });
        0 < e.length && (t = e[0], e = i.GD.cutomAdDataList.filter(function(t) {
            return t.tag == n;
        }), s = {
            tag: n,
            idx: 0,
            count: 0
        }, 0 < e.length ? s = e[0] : i.GD.cutomAdDataList.push(s), 0 < t.rule.length && (t = t.rule[s.idx % t.rule.length], 
        s.count++, s.count >= t.count && (s.count = 0, s.idx++), i.GD.cutomAdDataList.forEach(function(t, e) {
            t.tag == n && (i.GD.cutomAdDataList[e] = s);
        })));
    }, n.prototype.getAdUnitId = function(e) {
        var t = o.GC.customAdUnitIds.filter(function(t) {
            return t.tag == e;
        });
        return 0 < t.length ? t[0].adUnitId : "adunit-094e622819927c29";
    }, n.prototype.getConfig = function(e) {
        var t = o.GC.customAdConfigList.filter(function(t) {
            return t.tag == e;
        });
        return 0 < t.length ? t[0] : null;
    }, n.prototype.getOption = function(t) {
        var e = this.getAdUnitId(t), t = o.GC.customAdOptionList.filter(function(t) {
            return t.adUnitId == e;
        });
        return 0 < t.length ? t[0] : null;
    }, n.prototype.getOptionByAdUnitId = function(e) {
        var t = o.GC.customAdOptionList.filter(function(t) {
            return t.adUnitId == e;
        });
        return 0 < t.length ? t[0] : null;
    }, n._manager = null, n;
}();

n.default = r;